const db = require('../db/index')
const bcrypt = require('bcryptjs')

// 获取用户基本信息
exports.getuserinfo = (req,res)=>{ 
    const sqlstr = 'select id, username,nickname,email,user_pic from yeb_user where id = ?'
    // console.log(req.user);
    db.query(sqlstr,req.user.id,(err,result)=>{
        if(err) return res.echo(err)
        if(result.length !== 1) { 
            res.echo('获取用户信息失败！')
        }
        res.send({ 
            status:0,
            message:'用户信息获取成功！',
            data:result[0]
        })
    })
}

// 更新用户基本信息
exports.updateuserinfo = (req,res)=>{ 
    const sqlstr1 = 'update yeb_user set nickname=?,email=? where id=?'
    db.query(sqlstr1,[req.body.nickname,req.body.email,req.user.id],(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.affectedRows !== 1 ) return res.echo('用户基本信息更新失败！')
        res.echo('用户信息更新成功！',0)
    })
}

// 重置密码的处理函数
exports.updatepwd = (req,res)=>{ 
    // 判断用户是否存在
    const sqlstr1 = 'select * from yeb_user where id = ?'
    db.query(sqlstr1,req.user.id,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length!==1) return res.echo('用户不存在！')
        // 判断新旧密码是否一致 利用bcrptjs来判断
        const compareresult = bcrypt.compareSync(req.body.oldPwd,result[0].password)
        if(!compareresult) return res.echo('原密码错误！')
        // 新旧密码一致，将新密码加密写入数据库
        const sqlstr2 = 'update yeb_user set password = ? where id = ?'
        const newPwd = bcrypt.hashSync(req.body.newPwd,10)
        db.query(sqlstr2,[newPwd,req.user.id],(err,result)=>{ 
            if(err) return res.echo(err)
            if(result.affectedRows !==1 ) return res.echo('更新密码失败！')
            res.echo('密码修改成功！',0)
        })
    })
}

// 跟新用户头像的处理函数
exports.updateAvatar = (req,res)=>{
    const sqlstr1 = 'update yeb_user set user_pic = ? where id = ?'
    db.query(sqlstr1,[req.body.user_pic,req.user.id],(err,result)=>{ 
        if( err ) return res.echo(err)
        if(result.affectedRows !== 1 ) return res.echo('头像更新失败')
        const sqlstr2 = 'update yeb_comment set user_pic = ? where user_id = ?'
        db.query(sqlstr2,[req.body.user_pic,req.user.id],(err,result)=>{
            if(err) return res.echo(err) 
            if(result.affectedRows === 0) return res.echo('数据更新失败')
            res.echo('头像更新成功！',0)
        })
    })
}

// 用户发布商品
exports.userpublish = (req,res) => {
    const insertinfo = {...req.body,user_id:req.user.id,Author:req.user.username}
    const sqlstr = 'insert into yeb_product_info set ? '
    db.query(sqlstr,insertinfo,(err,result)=> {
        if(err) return res.echo(err)
        if(result.affectedRows === 0) return res.echo('插入数据失败') 
        res.echo('插入数据成功',0)
    })
}

// 根据用户id查询用户发布且审核通过的商品
exports.getuserproduct = (req,res) => {
    const sqlstr = 'select * from yeb_product_info where user_id = ? and isAudit = 1'
    db.query(sqlstr,req.user.id,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.length === 0) return res.echo('暂无商品信息')
        res.send({
            status:0,
            message:'商品查询成功',
            data : result
        })
    })
}

// 根据用户id查询用户发布且待审核的商品
exports.getuserproductisAudit = (req,res) => {
    const sqlstr = 'select * from yeb_product_info where user_id = ? and isAudit = 0'
    db.query(sqlstr,req.user.id,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.length === 0) return res.echo('暂无商品信息')
        res.send({
            status:0,
            message:'商品查询成功',
            data : result
        })
    })
}

// 根据用户id查询用户发布且未通过审核的商品
exports.getuserproductaddreason = (req,res) => {
    const sqlstr = 'select * from yeb_product_info where user_id = ? and isAudit = 2'
    db.query(sqlstr,req.user.id,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.length === 0) return res.echo('暂无商品信息')
        res.send({
            status:0,
            message:'商品查询成功',
            data : result
        })
    })
}